package cn.dengta.webapp.user.model;

import javax.annotation.Nonnull;

import cn.dengta.common.model.ValueProxy;
import cn.dengta.common.model.ValueProxyImpl;
import lombok.Getter;

@Getter
public enum CredentialType implements ValueProxyImpl<CredentialType, Integer> {

	USERNAME("用户名", "username", "status", 0),
	PHONE("手机号码", "phone", "phone_status", 1, true),
	EMAIL("邮箱", "email", "email_status", 1, true);

	public final String label;
	public final boolean canSendSms;
	public final String field;
	public final String statusField;
	public final Integer statusValue;

	final ValueProxy<CredentialType, Integer> proxy;

	CredentialType(String label, String field, String statusField, Integer statusValue, boolean canSendSms) {
		this.label = label;
		this.field = field;
		this.canSendSms = canSendSms;
		this.statusField = statusField;
		this.statusValue = statusValue;
		this.proxy = new ValueProxy<>(this, ordinal(), label);
	}

	CredentialType(String label, String field, String statusField, Integer statusValue) {
		this(label, field, statusField, statusValue, false);
	}

	@Nonnull
	public static CredentialType identify(String identifier) {
		if (PhoneArea.match(identifier)) { // 如果是手机号码
			return PHONE;
		} else if (identifier.indexOf('@') > 0) { // 如果是邮箱
			return EMAIL;
		} else { // 如果是用户名
			return USERNAME;
		}
	}

	public static CredentialType[] getValues() {
		return ValueProxy.getCachedArray(CredentialType.class, CredentialType::values);
	}

	public static CredentialType of(Integer value) {
		return PHONE.getValueOf(value);
	}
}
